package 力扣._6914_翻倍以链表形式表示的数字;

import 力扣.公用数据结构.ListNode;

public class Solution {
    ListNode reverseList(ListNode head) {
        if (head == null)
            return null;
        ListNode t = new ListNode(0); //头结点
        t.next = head;
        head = t;
        ListNode p = head.next;

        head.next = null;
        while (p != null) {
            t = p;
            p = p.next;
            t.next = head.next;
            head.next = t;
        }
        return head.next;
    }

    //先逆序，在翻倍，再逆序
    public ListNode doubleIt(ListNode head) {
        ListNode t = reverseList(head);
        ListNode r = t;
        int jinwei = 0;
        while (r != null) {
            r.val = r.val * 2 + jinwei;
            jinwei = r.val / 10;
            r.val %= 10;
            if (r.next == null)
                break;
            else
                r = r.next;
        }
        if (jinwei != 0) {
            ListNode node = new ListNode(jinwei);
            r.next = node;
            node.next = null;
        }

        return reverseList(t);
    }


}
